package com.example.touch.mediator;

import java.util.ArrayList;
import java.util.Collection;

import android.app.Activity;

import com.example.touch.communication.entities.ActionList;
import com.example.touch.communication.entities.ContextAwareModel;
import com.example.touch.communication.entities.Event;
import com.example.touch.communication.entities.TimeTable;
import com.example.touch.domain.EventActionRule;
import com.example.touch.domain.EventTrigger;
import com.example.touch.domain.IActivityAction;
import com.example.touch.domain.ICompositeAction;

public class EventActionAssembler 
{
	private Collection<ActionList> actionLists;
	private Collection<Event> events;
	private Collection<TimeTable> timeTables;
	private ActionMapper actionMapper;
	private EventTriggerMapper eventTriggerMapper;
	
	public EventActionAssembler(Activity currentActivity, ContextAwareModel semanticModel)
	{
		this.actionLists = semanticModel.getActions();
		this.events = semanticModel.getEvents();
		this.timeTables = semanticModel.getTimeTables();
		this.actionMapper = new ActionMapper(currentActivity);
		this.eventTriggerMapper = new EventTriggerMapper(timeTables);
	}
	
	public Collection<EventActionRule> resolveSemanticModel()
	{
		Collection<EventActionRule> eventActionRules = new ArrayList<EventActionRule>();
        
        for(Event e : events)
        {
            String eId = e.getId();
            EventActionRule r = new EventActionRule();
            r.setEventID(eId);
            
            Collection<EventTrigger> eventTriggers = eventTriggerMapper.mapEventTriggers(e);
            r.setEventTriggers(eventTriggers);
            
            // Resolve references
            for(ActionList al : actionLists)
            {
                if(al.getEventID().equalsIgnoreCase(eId))
                {
                	ICompositeAction actionSequence = actionMapper.mapActionList(al);
                    r.addAction(actionSequence);
                }
            }

            IActivityAction embeddedAction = handleEmbeddedAction(e);
            if(embeddedAction != null)
            {
            	r.addAction(embeddedAction);
            }
            
            eventActionRules.add(r);
        }
        
        return eventActionRules;
	}
	
	private IActivityAction handleEmbeddedAction(Event e)
	{
		String actionType = e.getAction();
		IActivityAction embeddedAction = null;
		
		if(actionType != null && !actionType.isEmpty())
        {	
        	if(actionType.equalsIgnoreCase("vibrate"))
        	{
        		embeddedAction = actionMapper.getActionFactory().createVibrateAction(e.getDuration());
        	}
        	else if(actionType.equalsIgnoreCase("notification"))
        	{
        		embeddedAction = actionMapper.getActionFactory().createNotificationAction(e.getMessage());
        	}
        	else if(actionType.equalsIgnoreCase("openbrowser"))
        	{
        		embeddedAction = actionMapper.getActionFactory().createOpenBrowserAction(e.getBrowserUrl());
        	}
        	else if(actionType.equalsIgnoreCase("sound"))
        	{
        		embeddedAction = actionMapper.getActionFactory().createSoundAction(e.getSoundSource());
        	}
        	else if(actionType.equalsIgnoreCase("media"))
        	{
        		embeddedAction = actionMapper.getActionFactory().createMediaAction(e.getMediaSource());
        	}
        }
		
		return embeddedAction;
	}
}
